package com.micro.frog.acct.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.frog.acct.dao.DictMapper;
import com.micro.frog.acct.entity.Dict;
import com.micro.frog.acct.service.DictService;
import com.micro.frog.acct.vo.DictVO;
import com.micro.frog.core.common.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author :  ljh
 * @date : 2018/4/11
 **/
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    DictMapper dictMapper;

    @Override
    public List<Dict> all(Dict dict) {
        return dictMapper.selectByCodition(dict);
    }

    @Override
    public PageResult<Dict> listPage(DictVO dictVO) {
        dictVO.init();
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictVO,dict);
        PageResult<Dict> pageResult = new PageResult<>();
        Page page = PageHelper.startPage(dictVO.getPageNum(),dictVO.getPageSize(),true);
        List<Dict> list = dictMapper.selectByCodition(dict);
        pageResult.setCode("10000");
        pageResult.setMsg("success");
        pageResult.setData(list);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    public int add(Dict dict) {
        dict.setIsDeleted("N");
        dict.setCreatedTime(new Date());
        return dictMapper.insertSelective(dict);
    }

    @Override
    public int delete(Integer id) {
        Dict dict = new Dict();
        dict.setId(id);
        dict.setIsDeleted("Y");
        return dictMapper.updateByPrimaryKeySelective(dict);
    }

    @Override
    public int update(Dict dict) {
        dict.setUpdatedTime(new Date());
        return dictMapper.updateByPrimaryKeySelective(dict);
    }
}
